import React from "react";
import {NavigationParams, NavigationScreenProp} from "react-navigation";
import {Platform} from "react-native";
import {globalStore} from "../../libs/ShareStore";


export type ANavigationComponentState = {
}

export type ANavigationComponentProp = {
    paramsKey?:string
    navigation?:NavigationScreenProp<ANavigationComponentState, NavigationParams>
}


export default class ANavigationComponent<P extends ANavigationComponentProp, S extends ANavigationComponentState>
    extends React.Component<P, S> {

    constructor(props:any){
        super(props);
        this.setPageRef();
    }

    public setPageRef(){
        const navigationParams = this.getNavigationParams();
        if(navigationParams !== undefined){
            navigationParams['ref'] = this;
        }
    }

    public getNavigationParams():any{
        if((!this.props.paramsKey && Platform.OS === 'web') ||
            (!this.props.navigation && Platform.OS !== 'web')){
            return {};
        }
        if(Platform.OS === 'web'){
            const paramsKV = globalStore.webNavigatorKeyValue[this.props.paramsKey];
            if(!paramsKV){
                return {};
            }
            return globalStore.webNavigatorKeyValue[this.props.paramsKey];
        }
        else if(Platform.OS === 'android' || Platform.OS === 'ios'){
            return this.props.navigation!.state.params;
        }
    }

    public getNavigationParam(key:string):any{
        if((!this.props.paramsKey && Platform.OS === 'web') ||
            (!this.props.navigation && Platform.OS !== 'web')){
            return;
        }
        if(Platform.OS === 'web'){
            const paramsKV = globalStore.webNavigatorKeyValue[this.props.paramsKey];
            if(!paramsKV){
                return null;
            }
            return globalStore.webNavigatorKeyValue[this.props.paramsKey][key];
        }
        else if(Platform.OS === 'android' || Platform.OS === 'ios'){
            return this.props.navigation!.getParam(key)
        }
    }

}
